<?php
require(dirname(__FILE__) . '/base.class.php');
class Adv extends Base
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    // 获取分类列表
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql = "select * from advs limit {$start},{$page_num}";
        $list = $this->db->query($sql);
        if ($list) {
            // 将图片路径处理成绝对路径
            foreach ($list as $key => $item) {
                $list[$key]['img'] = path($item['img']);
            }
            $output['data'] = $list;
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取广告列表失败';
        }
        echo json_encode($output);
    }


    // 添加广告
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            if (isset($_POST['page'])) {
                $data['page'] = $_POST['page'];
            }
            if (isset($_POST['name'])) {
                $data['name'] = $_POST['name'];
            }
            if (isset($_POST['title'])) {
                $data['title'] = $_POST['title'];
            }
            if (isset($_POST['img'])) {
                $data['img'] = $_POST['img'];
            }
            if (isset($_POST['url'])) {
                $data['url'] = $_POST['url'];
            }
            if (isset($_POST['status'])) {
                $data['status'] = $_POST['status'];
            }
            $data['createtime'] = date('Y-m-d H:i:s');
            $query_str = arr2str($data);
            $sql = "insert into advs set $query_str";
            $res = $this->db->query($sql);
            if ($res) {
                // 将广告id返回
                $output['id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }

    /*
        更新广告信息
    */ 
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        $id = $_GET['id'];
        if ($_POST) {
            if (isset($_POST['page'])) {
                $data['page'] = $_POST['page'];
            }
            if (isset($_POST['name'])) {
                $data['name'] = $_POST['name'];
            }
            if (isset($_POST['title'])) {
                $data['title'] = $_POST['title'];
            }
            if (isset($_POST['img'])) {
                $data['img'] = $_POST['img'];
            }
            if (isset($_POST['url'])) {
                $data['url'] = $_POST['url'];
            }
            if (isset($_POST['status'])) {
                $data['status'] = $_POST['status'];
            }
            $query_str = arr2str($data);
            $sql = "update into advs set $query_str where id={$id}";
            $res = $this->db->query($sql);
            if (!$res) {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }
    /*
        获取详情
    */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($id = $_GET['id']) {
            $sql = "select * from advs where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['img'] = path($res['img']);
                $output['data'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '数据不存在';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    /*
        删除广告
    */ 
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "delete from advs where id={$id}";
            $res = $this->db->query($sql);
            if (!$res) {
                $output['errcode'] = 3;
                $output['msg'] = '删除失败';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
}
